Check Armstrong Number Using C++

03-11-17 Course- CPP

A positive integer is called an Armstrong number if the sum of cubes of individual digit is equal to that number itself. For example:


153 = 1*1*1 + 5*5*5 + 3*3*3  // 153 is an Armstrong number.
12 is not equal to 1*1*1+2*2*2  // 12 is not an Armstrong number.

Source Code to Check Armstrong Number


/* C++ program to check whether a number entered by user is Armstrong or not. */

#include <iostream>
using namespace std;
int main()
{
  int n, n1, rem, num=0;
  cout << "Enter a positive  integer: ";
  cin >> n;
  n1=n;
  while(n1!=0)
  {
      rem=n1%10;
      num+=rem*rem*rem;
      n1/=10;
  }
  if(num==n)
    cout << n << " is an Armstrong number.";
  else
    cout << n << " is not an Armstrong number.";
  return 0;
}

Output


Enter a positive integer: 371
371 is an Armstrong number.